package cn.avalon.order.service.impl;

import cn.avalon.basic.service.impl.BaseServiceImpl;
import cn.avalon.basic.utils.LoginContext;
import cn.avalon.basic.utils.PageList;
import cn.avalon.order.domain.OrderPetAcquisition;
import cn.avalon.order.dto.OrderDto;
import cn.avalon.order.mapper.OrderPetAcquisitionMapper;
import cn.avalon.order.query.OrderPetAcquisitionQuery;
import cn.avalon.order.service.IOrderPetAcquisitionService;
import cn.avalon.org.domain.Employee;
import cn.avalon.org.domain.Shop;
import cn.avalon.org.mapper.EmployeeMapper;
import cn.avalon.org.mapper.ShopMapper;
import cn.avalon.pet.domain.Pet;
import cn.avalon.pet.domain.SearchMasterMsg;
import cn.avalon.pet.mapper.PetMapper;
import cn.avalon.pet.mapper.SearchMasterMsgMapper;
import cn.avalon.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;

    @Override
    @Transactional
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, HttpServletRequest request) {

        Logininfo logininfo = LoginContext.getLogininfo(request);

        Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());

        // 判断登录员工是那种角色
        //1.employee中shop_id为null，平台管理员
        Long shopId = employee.getShop_id();

        if(employee.getShop_id() != null){//店铺管理员【店长 or 店员】
            query.setShopId(employee.getShop_id());
            Shop shop = shopMapper.findById(employee.getShop_id());
            //不等于店员，等于就是店长
            if(shop.getAdminId()!=employee.getId()){
                query.setEmpId(employee.getId());
            }
        }
        //5.平台管理员
        return super.queryPage(query);
    }

    @Override
    public void confirm(OrderDto orderDto) {
        //1.改订单状态 0待处理 - 1已处理
        //2.修改订单的支付方式和金
        OrderPetAcquisition order = orderPetAcquisitionMapper.findById(orderDto.getId());
        order.setState(1);
        order.setPaytype(orderDto.getPayType());
        order.setPrice(orderDto.getMoney());
        orderPetAcquisitionMapper.update(order);

        //3.改寻主消息状态 1待处理 -2已完成
        SearchMasterMsg msg = searchMasterMsgMapper.findById(order.getSearchMasterMsgId());
        msg.setState(2);
        searchMasterMsgMapper.update(msg);

        //4.修改pet的成本价
        Pet pet = petMapper.findById(order.getPetId());
        pet.setCostprice(orderDto.getMoney());
        petMapper.update(pet);
    }

    @Override
    public void cancel(Long id) {
        OrderPetAcquisition order = orderPetAcquisitionMapper.findById(id);
        //1.shopId置空
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());

        //2.删除宠物信息 - 订单取消了，宠物就没有接回来。之前已经将宠物录入系统了
        petMapper.remove(order.getPetId());

        //3.改状态
        order.setState(-1);
        orderPetAcquisitionMapper.update(order);
    }
}
